package com.czf.personal.controller;

import com.czf.common.entity.FilePathInfo;
import com.czf.common.util.AjaxResult;
import com.czf.personal.service.FileUploadServiceImpl;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author chenzhanfan
 * @Date 2022/3/20
 * @Description
 */
@RestController
@CrossOrigin
@Slf4j
public class FileUploadController {

    @Autowired
    private FileUploadServiceImpl fileUploadService;

    @RequestMapping(value = "/fileUpload",method = RequestMethod.POST,headers = "content-type=multipart/form-data")
    public AjaxResult fileUpload(
            @ApiParam(value = "上传文件",required = true)
            @RequestPart("file") MultipartFile file,Long userId,String purpose,String remarks,Boolean personalOrEnterprise) {
        Object upload = fileUploadService.upload(file, userId, purpose, remarks, personalOrEnterprise);
        if (ObjectUtils.isNotEmpty(upload)){
            return AjaxResult.success(upload);
        }
        return AjaxResult.error();
    }

    @RequestMapping(value = "/fileGet",method = RequestMethod.GET)
    public AjaxResult getEightRecord(Long userId){
        log.info("userId为：{}",userId);
        List<FilePathInfo> record = fileUploadService.getRecord(userId);
        if (CollectionUtils.isEmpty(record)) {
            return AjaxResult.success();
        } else {
            return AjaxResult.success(record);
        }
    }
}
